<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑订单 - 电动车销售系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/salesadmin/editsale.css">
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">销售管理 - 编辑订单</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="d-none d-md-inline">${currentUser.name}</span>
                    <div class="badge bg-light text-dark">
                        <i class="bi bi-person-circle me-1"></i>
                        <c:choose>
                            <c:when test="${currentUser.role == 4}">系统管理员</c:when>
                            <c:when test="${currentUser.role == 0}">业务员</c:when>
                            <c:when test="${currentUser.role == 1}">仓库管理员</c:when>
                            <c:when test="${currentUser.role == 2}">销售管理员</c:when>
                            <c:when test="${currentUser.role == 3}">售后管理员</c:when>
                        </c:choose>
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-gear fs-5"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/change_password.jsp"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/LogoutServlet"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="container my-4">
    <div class="row justify-content-center">
        <div class="col-lg-9">
            <div class="card shadow-lg">
                <div class="card-header bg-primary text-white py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="m-0"><i class="bi bi-pencil-square me-2"></i>编辑订单 #${sale.id}</h5>
                        <a href="${pageContext.request.contextPath}/salesAdmin?action=list"
                           class="btn btn-light">
                            <i class="bi bi-arrow-left me-1"></i> 返回列表
                        </a>
                    </div>
                </div>

                <div class="card-body p-4">
                    <c:if test="${not empty errorMsg}">
                        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMsg}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    </c:if>

                    <div class="info-box">
                        <p><i class="bi bi-info-circle"></i> 修改订单信息后，请确保所有数据准确无误</p>
                        <p><i class="bi bi-info-circle"></i> 订单状态更改后，系统将自动通知相关人员</p>
                    </div>

                    <div class="order-summary mb-4">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="mb-0"><i class="bi bi-card-checklist me-2"></i>订单摘要</h5>
                            <span class="status-badge ${sale.status == 0 ? 'status-pending' : 'status-approved'}">
                                ${sale.status == 0 ? '未出库' : '已出库'}
                            </span>
                        </div>

                        <div class="summary-item">
                            <span class="summary-label">创建时间:</span>
                            <span class="summary-value">${saleTimeStr}</span>
                        </div>
                        <div class="summary-item">
                            <span class="summary-label">最后更新:</span>
                            <span class="summary-value">${lastUpdatedStr}</span>
                        </div>
                        <div class="summary-item">
                            <span class="summary-label">操作人员:</span>
                            <span class="summary-value">${currentUser.name}</span>
                        </div>
                    </div>

                    <form action="${pageContext.request.contextPath}/salesAdmin?action=update" method="post">
                        <input type="hidden" name="id" value="${sale.id}">

                        <div class="form-section">
                            <div class="d-flex align-items-center mb-3">
                                <div class="section-icon">
                                    <i class="bi bi-person"></i>
                                </div>
                                <h5 class="form-section-title m-0">客户信息</h5>
                            </div>

                            <div class="mb-3">
                                <label for="customerId" class="form-label required-label">客户ID</label>
                                <input type="number" class="form-control form-control-lg" id="customerId"
                                       name="customerId" value="${sale.customerId}" required>
                            </div>
                        </div>

                        <div class="form-section">
                            <div class="d-flex align-items-center mb-3">
                                <div class="section-icon">
                                    <i class="bi bi-person-badge"></i>
                                </div>
                                <h5 class="form-section-title m-0">员工信息</h5>
                            </div>

                            <div class="mb-3">
                                <label for="userId" class="form-label required-label">销售员ID</label>
                                <input type="number" class="form-control form-control-lg" id="userId"
                                       name="userId" value="${sale.userId}" required>
                            </div>
                        </div>

                        <div class="form-section">
                            <div class="d-flex align-items-center mb-3">
                                <div class="section-icon">
                                    <i class="bi bi-bicycle"></i>
                                </div>
                                <h5 class="form-section-title m-0">车辆信息</h5>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="vehicleId" class="form-label required-label">车辆ID</label>
                                    <input type="number" class="form-control form-control-lg" id="vehicleId"
                                           name="vehicleId" value="${sale.vehicleId}" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="quantity" class="form-label required-label">数量</label>
                                    <input type="number" class="form-control form-control-lg" id="quantity"
                                           name="quantity" value="${sale.quantity}" required min="1">
                                </div>
                            </div>
                        </div>

                        <div class="form-section">
                            <div class="d-flex align-items-center mb-3">
                                <div class="section-icon">
                                    <i class="bi bi-calendar-event"></i>
                                </div>
                                <h5 class="form-section-title m-0">订单详情</h5>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="saleTime" class="form-label required-label">销售时间</label>
                                    <input type="datetime-local" class="form-control form-control-lg" id="saleTime"
                                           name="saleTime" value="${saleTimeStr}" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="status" class="form-label required-label">订单状态</label>
                                    <select class="form-select form-select-lg" id="status" name="status" required>
                                        <option value="0" ${sale.status == 0 ? "selected" : ""}>未出库</option>
                                        <option value="1" ${sale.status == 1 ? "selected" : ""}>已出库</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="action-buttons">
                            <button type="submit" class="btn btn-submit">
                                <i class="bi bi-check-circle me-1"></i> 更新订单
                            </button>
                            <a href="${pageContext.request.contextPath}/salesAdmin?action=list"
                               class="btn btn-cancel">
                                <i class="bi bi-x-circle me-1"></i> 取消返回
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 状态更改提示
    document.getElementById('status').addEventListener('change', function() {
        const status = this.value;
        if (status == 1) {
            alert('订单状态改为"已出库"后，系统将自动通知仓库发货，请确保库存充足！');
        }
    });

    // 自动关闭消息提示
    document.querySelectorAll('.alert .btn-close').forEach(button => {
        button.addEventListener('click', function() {
            this.closest('.alert').style.display = 'none';
        });
    });

    // 5秒后自动关闭消息提示
    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(alert => {
            alert.style.display = 'none';
        });
    }, 5000);

    // 设置当前时间为默认值（如果没有值）
    document.addEventListener('DOMContentLoaded', function() {
        const saleTimeInput = document.getElementById('saleTime');
        if (!saleTimeInput.value) {
            const now = new Date();
            const localDateTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString().slice(0, 16);
            saleTimeInput.value = localDateTime;
        }
    });
</script>
</body>
</html>